from rest_framework.generics import RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView

from doctor.models import Doctoc
from .serializers import DoctorInfoSerializer

class DoctorMyInfor(APIView):
    permission_classes = [IsAuthenticated]
    def get(self,request):
        doctor = Doctoc.objects.get(user=request.user)
        serializer = DoctorInfoSerializer(instance=doctor)
        return Response(serializer.data)


class GreateDoctor(APIView):
    """
    医生个人中心界面只能是 登录用户访问, 前端 通过在header中 添加token

    # 1. 这个接口只能用登录用户访问
    # 2. 获取登录用户的信息 user
    # 3. 创建序列化器
    # 4. 返回数据
    Post     doctor/create/
    """
    # 1. 这个接口只能用登录用户访问
    permission_classes = [IsAuthenticated]

    def post(self, request):
        data = request.data
        data["user"] = request.user.id
        serializer = DoctorInfoSerializer(data=data)
        # 4. 返回数据
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)


class UpdateDoctor(APIView):
    permission_classes = [IsAuthenticated]

    def put(self, request):
        data = request.data
        doctor = Doctoc.objects.get(user=request.user)
        seriazlier = DoctorInfoSerializer(instance=doctor, data=data, partial=True)
        seriazlier.is_valid()
        seriazlier.save()

        return Response(seriazlier.data)


from .serializers import DoctorIndexSerializer
from drf_haystack.viewsets import HaystackViewSet


class DoctorSearchViewSet(HaystackViewSet):
    """
    doctor搜索
    """
    permission_classes = [IsAuthenticated]

    index_models = [Doctoc]

    serializer_class = DoctorIndexSerializer
